package com.testDemo.service;

import javax.annotation.Resource;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.testDemo.dao.TestDao;
import com.testDemo.enrty.AopTestException;
import com.testDemo.enrty.TestDemoResult;
import com.testDemo.enrty.TestLog;
import com.testDemo.utils.TestUtils;

@Service
@Transactional
public class RequestServiceImpl implements RequestService {

    @Resource
    private TestDao dao;

    Logger log = Logger.getLogger(RequestServiceImpl.class);

    @Override
    public TestDemoResult doClient(String url, String appkey, String bizkey, String method, String msg) {
        url = (String) TestUtils.urlMap.get(url);
        System.out.println("调用地址 ： " + url);
        if (TestUtils.isEmpty(url)) {
            throw new AopTestException(AopTestException.ERROR_CODE, "未获取到对应的地址");
        }
        method = method.substring(0, method.indexOf("（"));

        String result = "";
        GetMethod postMethod = null;
        String postUrl = "";
        String apptx = "AOPTEST" + TestUtils.getSysDate();
        String timestamp = TestUtils.getSysDateFormat();
        String rsp = "";
        try {
            // String bizkey = GetBizkeyUtils.getBizkeyByMethod(method).get(0);
            postUrl = url + "?apptx=" + apptx + "&appkey=" + appkey + "&method=" + method + "&msg=" + msg
                    + "&timestamp=" + timestamp + "&bizkey=" + bizkey;
            log.info(apptx + " 发送请求:" + postUrl);
            postMethod = new GetMethod(postUrl);
            HttpClient client = new HttpClient();
            // client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间
            client.executeMethod(postMethod);
            rsp = postMethod.getResponseBodyAsString();
            result = "<span>流水号:</span><input type='text' value='" + apptx + "'/><br/>";
            result += "<span>请求报文:</span><div id='req'>" + postUrl + "</div>";
            result += "<span>返回报文:</span><div id='rsp'>" + rsp + "</div>";
            log.info(apptx + " 返回:" + result);
        }
        catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
            result = e.toString().replaceAll("&", "&amp;");
            rsp = result;
            log.info(apptx + " 异常信息:" + result);
        }
        // 使用finally块来关闭连接
        finally {
            postMethod.releaseConnection();
        }
        try {
            // 生成日志实体
            TestLog log = new TestLog(apptx, appkey, method, bizkey, msg, timestamp, rsp, "", "");
            dao.insertTestLog(log);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.info(apptx + " 记录请求信息失败 :" + e.getMessage());
        }
        return new TestDemoResult(TestDemoResult.SUCCESS_CODE, "", result);
    }
}
